#include"Field.h" 

#include"FieldBush.h"
#include"FieldForest.h"
#include"FieldGrass.h"
#include"FieldHills.h"
#include"FieldRocks.h"
#include"FieldSwamp.h"
#include"FieldWater.h"

#include<assert.h>

Field *Field::getField(FieldType type, int r, int c){
	switch(type){
		case FIELD_GRASS:
			return new FieldGrass(r,c);
		case FIELD_ROCKS:
			return new FieldRocks(r,c);
		case FIELD_HILLS:
			return new FieldHills(r,c);
		case FIELD_SWAMP:
			return new FieldSwamp(r,c);
		case FIELD_WATER:
			return new FieldWater(r,c);
		case FIELD_FOREST:
			return new FieldForest(r,c);
		case FIELD_BUSH:
			return new FieldBush(r,c);
	}
	assert(0);//WARNING This point should never be reached.
	return new FieldGrass(r,c);
}

Field::Field(int r, int c){
	m_row = r;
	m_col = c;
	m_occupiedBy = 0;
}

Field::~Field(){
	
}

void Field::addNeighbour(Field *other){
	if(!m_neighbours.contains(other))
		m_neighbours.push_back(other);
}
// #include<stdio.h>
int Field::distance(const Field *other)const{
	int x = col();
	int y = row();
	int len = 0;
	while(y != other->row()){
		if(y < other->row())
			y++;
		else
			y--;
		if(x > other->col() && y%2==1)
			x--;
		if(x < other->col() && y%2==0)
			x++;
		len++;
	}
	len += x > other->col() ? x - other->col() : other->col() - x;
	return len;
	/*
	int ret = 0;
	if(row() > other->row())
		ret += row() - other->row();
	else
		ret += other->row() - row();
	
	if(col() > other->col())
		ret += col() - other->col();
	else
		ret += other->col() - col();
	
// 	printf("Distance between (%d,%d) and (%d,%d) is %d\n", row(), col(), other->row(), other->col(), ret);
	
	return ret;*/
}
